/***************************************************************************
 *   Copyright (C) 2009 - 2010 by Simon Qian <SimonQian@SimonQian.com>     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef __VSFIP_HTTPC_H_INCLUDED__
#define __VSFIP_HTTPC_H_INCLUDED__

struct vsfip_httpc_t
{
	uint32_t data_length;
	uint32_t resp_length;

	// private
	struct vsfsm_pt_t local_pt;

	struct vsfip_socket_t *so;
	struct vsfip_sockaddr_t hostip;

	const char *host;
	struct vsfip_buffer_t *buf;
	uint32_t resp_curptr;
	uint8_t *resp_type;
	uint8_t resp_code;
	uint16_t port;
};

vsf_err_t vsfip_httpc_connect(struct vsfsm_pt_t *pt, vsfsm_evt_t evt,
		const char *host);
vsf_err_t vsfip_httpc_disconnect(struct vsfsm_pt_t *pt, vsfsm_evt_t evt);

vsf_err_t vsfip_httpc_run_stream(struct vsfsm_pt_t *pt, vsfsm_evt_t evt,
		const char *verb, const char *path, const char *content_type,
		uint32_t content_length, struct vsf_stream_t *tx_stream,
		struct vsf_stream_t *rx_stream);
vsf_err_t vsfip_httpc_get_stream(struct vsfsm_pt_t *pt, vsfsm_evt_t evt,
		const char *path, struct vsf_stream_t *rx_stream);
vsf_err_t vsfip_httpc_post_stream(struct vsfsm_pt_t *pt, vsfsm_evt_t evt,
		const char *path, const char *content_type, uint32_t content_length,
		struct vsf_stream_t *tx_stream, struct vsf_stream_t *rx_stream);

struct vsfip_httpc_buf_t
{
	struct vsfip_httpc_t httpc;
	struct vsf_bufstream_t tx_bufstream;
	struct vsf_bufstream_t rx_bufstream;
	bool inited;
};
void vsfip_httpc_buf_init(struct vsfip_httpc_buf_t *httpc);
vsf_err_t vsfip_httpc_run_buf(struct vsfsm_pt_t *pt, vsfsm_evt_t evt,
		const char *verb, const char *path, const char *content_type,
		uint32_t content_length, struct vsf_buffer_t *tx_buffer,
		struct vsf_buffer_t *rx_buffer);
vsf_err_t vsfip_httpc_get_buf(struct vsfsm_pt_t *pt, vsfsm_evt_t evt,
		const char *path, struct vsf_buffer_t *rx_buffer);
vsf_err_t vsfip_httpc_post_buf(struct vsfsm_pt_t *pt, vsfsm_evt_t evt,
		const char *path, const char *content_type, uint32_t content_length,
		struct vsf_buffer_t *tx_buffer, struct vsf_buffer_t *rx_buffer);

#endif		// __VSFIP_HTTPD_H_INCLUDED__
